﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Krile.Imported.Controls
{
    public partial class ExCheckedListBox : CheckedListBox
    {
        public ExCheckedListBox() : base() { }

        public void ListClear()
        {
            this.Items.Clear();
        }

        public void ListItemAdd(object obj)
        {
            this.Items.Add(obj, true);
        }

        public int ListItemCount
        {
            get { return this.Items.Count; }
        }

        public void CheckAll()
        {
            for (int i = 0; i < this.Items.Count; i++)
            {
                this.SetItemChecked(i, true);
            }
        }

        public void UncheckAll()
        {
            for (int i = 0; i < this.Items.Count; i++)
            {
                this.SetItemChecked(i, false);
            }
        }

        public Type[] GetSelectedItem<Type>()
        {
            List<Type> retArray = new List<Type>();
            foreach (object obj in this.CheckedItems)
            {
                if (obj is Type)
                    retArray.Add((Type)obj);
            }
            return retArray.ToArray();
        }

        public void SetSelectedItem<Type>(Type[] selects)
        {
            SetSelectedItem<Type>(selects, true);
        }

        public void SetSelectedItem<Type>(Type[] selects, bool raiseException)
        {
            UncheckAll();
            foreach (Type cobj in selects)
            {
                if (!this.Items.Contains(cobj))
                {
                    if (raiseException)
                        throw new KeyNotFoundException("ExCheckedListBox::Item not existed.");
                    else
                        continue;
                }
                this.SetItemChecked(this.Items.IndexOf(cobj), true);
            }
        }

    }
}
